/**
 * Copyright (C) 2011-2012 GreenSoft S.A.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.com.greensoft.neosystem.user;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.ManagedBean;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import ar.com.greensoft.jeecommons.web.jsf.export.ColumnValueMapper;
import ar.com.greensoft.jeecommons.web.jsf.export.ExportColInfo;
import ar.com.greensoft.jeecommons.web.jsf.export.impl.ExportColInfoImpl;
import ar.com.greensoft.neosystem.dao.GenericDao;
import ar.com.greensoft.neosystem.dao.LocalidadDao;
import ar.com.greensoft.neosystem.dao.ProvinciaDao;
import ar.com.greensoft.neosystem.model.address.Localidad;
import ar.com.greensoft.neosystem.model.address.Pais;
import ar.com.greensoft.neosystem.model.address.Provincia;
import ar.com.greensoft.neosystem.search.SearchFilter;
import ar.com.greensoft.neosystem.search.address.LocalidadSearchFilter;

/**
 *
 * @author Jorge E. Villaverde
 * @since 1.0.0
 * @version 1.0.0
 */
@ManagedBean
@SessionScoped
@Named
public class LocalidadBean extends AbstractLongIdUserRoleCrudFacedeBean<Localidad>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6819598008271015179L;
	
	private static final String ENTITY_PREFIX = "direccion/localidad";
	private static final String ENTITY_MSG_PREFIX = "direccionLocalidad";
	
	
	@Inject
	private LocalidadDao dao;
	@Inject
	private ProvinciaDao provinciaDao;

	private Pais pais;
	
	private Provincia provincia;
	
	@Override
	protected GenericDao<Localidad, Long> getDao() {
		return dao;
	}

	@Override
	protected Localidad newEntity() {
		Localidad entity = new Localidad();
		entity.setProvincia(provincia);
		return entity;
	}

	@Override
	protected String entityPrefix() {
		return ENTITY_PREFIX;
	}

	@Override
	protected String getEntityMessagePrefix(){
		return ENTITY_MSG_PREFIX;
	}
	
	@Override
	protected SearchFilter createSearchFilter() {
		return new LocalidadSearchFilter();
	}

	@Override
	public LocalidadSearchFilter getSearchFilter(){
		return (LocalidadSearchFilter)super.getSearchFilter();
	}
	
	@Override
	protected String getEntityName(Localidad entity) {
		return entity.getName();
	}

	@Override
	protected Localidad findEntityByName(String entityName) {
		return dao.findByName(entityName);
	}
	
	@Override
	protected Localidad findEntity(Localidad entity){
		if(entity == null)
			return null;
		return dao.findByProvinciaName(entity.getProvincia(), entity.getName());
	}

	@Override
	protected List<ExportColInfo> getExportColInfoList() {
		List<ExportColInfo> ret = new ArrayList<ExportColInfo>();
		ret.add(new ExportColInfoImpl(0, "localidadPais"));
		ret.add(new ExportColInfoImpl(1, "localidadProvincia"));
		ret.add(new ExportColInfoImpl(2, "localidadNombre"));
		return ret;
	}

	@Override
	protected ColumnValueMapper<Localidad> getColumnValueMapper() {
		return new ColumnValueMapper<Localidad>() {
			@Override
			public String getColumnValue(int columnNumber, Localidad value) {
				switch (columnNumber) {
				case 0:
					return value.getProvincia().getPais().getName();
				case 1:
					return value.getProvincia().getName();
				case 2:
					return value.getName();	
				default:
					break;
				}
				return "";
			}
		};
	}

	public Pais getPais() {
		return pais;
	}

	public void setPais(Pais pais) {
		this.pais = pais;
	}

	public Provincia getProvincia() {
		return provincia;
	}

	public void setProvincia(Provincia provincia) {
		if(provincia != null){
			this.pais = provincia.getPais();
		}
		this.provincia = provincia;
	}
	
	public List<Provincia> getProvincias(){
		if(this.pais == null)
			return null;
		return provinciaDao.findByPais(pais);
	}

	@Override
	protected void beforeSearch() {
		super.beforeSearch();
		this.pais = null;
	}

	@Override
	public String showUpdate() {
		if(getEntity() != null && getEntity().getProvincia() != null)
			this.pais = getEntity().getProvincia().getPais();				
		return super.showUpdate();
	}	
}
